/* global window */
import pathToRegexp from 'path-to-regexp'
import modelExtend from 'dva-model-extend'
import { config } from 'utils'
import { routerRedux } from 'dva/router'
import { message } from 'antd'
import { get, brief, create, remove, downXMLFile } from 'services/report.js'
import { APIV4 } from 'utils/ip'
import { querylist as transQueryList } from 'services/transactionReceipt/transactionReceipt'
import * as basicData from 'services/basicData.js'
import { pageModel } from './common'


export default modelExtend(pageModel, {
  namespace: 'reportDetail',
  state: {
    editType: 'create',
    data: {},
    hasData: false,
    transactionsIds: [],
    currentId: '',
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((pathname) => {
        const match = pathToRegexp('/report/:id').exec(pathname.pathname)
        if (match) {
          if (match[1] !== config.ADDFLAG) {
            dispatch({ type: 'updateState', payload: { currentId: match[1] } })
            dispatch({ type: 'query', payload: { id: match[1] } })
          } else {
            dispatch({ type: 'getBrief' })
          }
        }
      })
    },
  },

  effects: {
    * query ({ payload = {} }, { call, put }) {
      const data = yield call(get, payload)
      if (data.code === 0) {
        yield put({
          type: 'updateState',
          payload: {
            editType: 'update',
            data: data.data,
            hasData: true,
          },
        })
      } else {
        throw { message: data.msg, code: data.code }
      }
    },
    * getBrief ({ payload = {} }, { call, put }) {
      const { data, code, msg } = yield call(brief, payload)
      const transListData = yield call(transQueryList, { state: '6' })
      if (code === 0 && transListData.code === 0) {
        data.transactions = transListData.data.data
        yield put({
          type: 'updateState',
          payload: {
            editType: 'create',
            data,
            hasData: true,
          },
        })
      } else {
        throw { message: (code === 0 ? transListData.msg : msg) }
      }
    },
    * create ({ payload = {} }, { call, put }) {
      const { data, code, msg } = yield call(create, payload)
      if (code === 0) {
        yield put(routerRedux.push({
          pathname: '/reports',
        }))
      } else {
        throw { message: msg, code }
      }
    },
    * delete ({ payload = {} }, { call, put, select }) {
      const state = yield select(_ => _.reportDetail)
      const { code, data, msg } = yield call(remove, { id: state.currentId })
      if (code === 0) {
        message.success('successfully deleted!')
        yield put(routerRedux.push({
          pathname: '/reports',
        }))
      } else {
        throw { message: msg, code }
      }
    },
    * downXMLFile ({ payload = {} }, { call, put, select }) {
      const state = yield select(_ => _.reportDetail)
      const { code, data, msg } = yield call(downXMLFile, { id: state.currentId })
      if (code === 0) {
        window.open(`${APIV4}/${data}`)
      } else {
        throw { message: msg || 'Network Error', code }
      }
    },
  },

  reducers: {

  },
})
